/**
 * 
 */
package sailing.app;

import java.awt.Dimension;

import javax.swing.JPanel;

import org.jdesktop.application.SingleFrameApplication;

import sailing.dao.AccountingSubjectDao;
import sailing.dao.BankAccountDao;
import sailing.dao.BankDepositsAccountDao;
import sailing.dao.BankNameDao;
import sailing.dao.BankStatementDao;
import sailing.dao.BillsDetailDao;
import sailing.dao.CashAccountDao;
import sailing.dao.CashVoucherDao;
import sailing.dao.CashierDao;
import sailing.dao.CustomerDao;
import sailing.dao.LoanAccountDao;
import sailing.dao.ReceivableDetailDao;
import sailing.dao.RunningAccountDao;
import sailing.dao.impl.AccountingSubjectDaoImpl;
import sailing.dao.impl.BankAccountDaoImpl;
import sailing.dao.impl.BankDepositsAccountDaoImpl;
import sailing.dao.impl.BankNameDaoImpl;
import sailing.dao.impl.BankStatementDaoImpl;
import sailing.dao.impl.BillsDetailDaoImpl;
import sailing.dao.impl.CashAccountDaoImpl;
import sailing.dao.impl.CashVoucherDaoImpl;
import sailing.dao.impl.CashierDaoImpl;
import sailing.dao.impl.CustomerDaoImpl;
import sailing.dao.impl.LoanAccountDaoImpl;
import sailing.dao.impl.ReceivableDetailDaoImpl;
import sailing.dao.impl.RunningAccountDaoImpl;
import sailing.db.DataSourceManagerImpl;
import sailing.managers.AccountingSubjectManager;
import sailing.managers.AppUIManager;
import sailing.managers.BankAccountManager;
import sailing.managers.BankDepositsAccountManager;
import sailing.managers.BankNameManager;
import sailing.managers.BankStatementManager;
import sailing.managers.BillsDetailManager;
import sailing.managers.CashAccountManager;
import sailing.managers.CashVoucherManager;
import sailing.managers.CashierManager;
import sailing.managers.CustomerManager;
import sailing.managers.LoanAccountManager;
import sailing.managers.ReceivableDetailManager;
import sailing.managers.RunningAccountManager;
import sailing.managers.impl.AccountingSubjectManagerImpl;
import sailing.managers.impl.BankAccountManagerImpl;
import sailing.managers.impl.BankDepositsAccountManagerImpl;
import sailing.managers.impl.BankNameManagerImpl;
import sailing.managers.impl.BankStatementManagerImpl;
import sailing.managers.impl.BillsDetailManagerImpl;
import sailing.managers.impl.CashAccountManagerImpl;
import sailing.managers.impl.CashVoucherManagerImpl;
import sailing.managers.impl.CashierManagerImpl;
import sailing.managers.impl.CustomerManagerImpl;
import sailing.managers.impl.LoanAccountManagerImpl;
import sailing.managers.impl.ReceivableDetailManagerImpl;
import sailing.managers.impl.RunningAccountManagerImpl;
import sailing.resource.Resource;
import sailing.ui.MainPanel;
import cjlite.IocConfigurator;
import cjlite.IocModule;
import cjlite.dao.DaoSqlSupportManager;
import cjlite.dao.DataSourceManager;
import cjlite.dao.core.DaoSqlSupportManagerImpl;
import cjlite.logger.Logger;
import cjlite.utils.Environment;

/**
 * Application Class
 * 
 * @author kevin
 * 
 */
public class SailingApplication extends SingleFrameApplication {

	private static Logger logger = Logger.thisClass();

	private static SailingApplication instance;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jdesktop.application.Application#startup()
	 */
	@Override
	protected void startup() {
		this.initialUI();
		this.initialWindowSize();
		show(this.getMainContentView());
		instance = this;
	}

	private void initialUI() {
		AppUIManager.getInstance().updateUI();
		System.out.println(Environment.getEvmentInfo());
	}

	private void initialWindowSize() {
		int windowW = this.getContext().getResourceMap().getInteger("MainWindow.defaultWidth");
		int windowH = this.getContext().getResourceMap().getInteger("MainWindow.defaultHeight");

		this.getMainFrame().setMinimumSize(new Dimension(1000, 600));
		this.getMainFrame().setSize(windowW, windowH);
		this.getMainFrame().setIconImage(Resource.getImageIcon("/ui/main/book_bookmarks_32.png").getImage());
	}

	private JPanel getMainContentView() {
		MainPanel mp = new MainPanel(this);
		return mp;
	}

	@Override
	protected IocModule getIocModule() {
		return new IocModule() {

			@Override
			public void configure(IocConfigurator configurator) {
				// Datasource Mgr and DaoSqlSupport Mgr
				configurator.addBean(DataSourceManager.class).to(DataSourceManagerImpl.class).asEagerSingleton();
				configurator.addBean(DaoSqlSupportManager.class).to(DaoSqlSupportManagerImpl.class).asEagerSingleton();

				configurator.addBean(CustomerManager.class).to(CustomerManagerImpl.class).asSingleton();
				configurator.addBean(CustomerDao.class).to(CustomerDaoImpl.class).asSingleton();

				configurator.addBean(CashVoucherManager.class).to(CashVoucherManagerImpl.class).asSingleton();
				configurator.addBean(CashVoucherDao.class).to(CashVoucherDaoImpl.class).asSingleton();

				configurator.addBean(BankDepositsAccountManager.class).to(BankDepositsAccountManagerImpl.class)
						.asSingleton();
				configurator.addBean(BankDepositsAccountDao.class).to(BankDepositsAccountDaoImpl.class).asSingleton();

				configurator.addBean(CashierManager.class).to(CashierManagerImpl.class).asSingleton();
				configurator.addBean(CashierDao.class).to(CashierDaoImpl.class).asSingleton();

				configurator.addBean(ReceivableDetailManager.class).to(ReceivableDetailManagerImpl.class).asSingleton();
				configurator.addBean(ReceivableDetailDao.class).to(ReceivableDetailDaoImpl.class).asSingleton();

				configurator.addBean(LoanAccountManager.class).to(LoanAccountManagerImpl.class).asSingleton();
				configurator.addBean(LoanAccountDao.class).to(LoanAccountDaoImpl.class).asSingleton();

				configurator.addBean(RunningAccountManager.class).to(RunningAccountManagerImpl.class).asSingleton();
				configurator.addBean(RunningAccountDao.class).to(RunningAccountDaoImpl.class).asSingleton();

				configurator.addBean(BankNameManager.class).to(BankNameManagerImpl.class).asSingleton();
				configurator.addBean(BankNameDao.class).to(BankNameDaoImpl.class).asSingleton();

				configurator.addBean(BankAccountManager.class).to(BankAccountManagerImpl.class).asSingleton();
				configurator.addBean(BankAccountDao.class).to(BankAccountDaoImpl.class).asSingleton();

				configurator.addBean(AccountingSubjectManager.class).to(AccountingSubjectManagerImpl.class)
						.asSingleton();
				configurator.addBean(AccountingSubjectDao.class).to(AccountingSubjectDaoImpl.class).asSingleton();

				configurator.addBean(CashAccountManager.class).to(CashAccountManagerImpl.class).asSingleton();
				configurator.addBean(CashAccountDao.class).to(CashAccountDaoImpl.class).asSingleton();
				
				configurator.addBean(BankStatementManager.class).to(BankStatementManagerImpl.class).asSingleton();
				configurator.addBean(BankStatementDao.class).to(BankStatementDaoImpl.class).asSingleton();
				
				configurator.addBean(BillsDetailManager.class).to(BillsDetailManagerImpl.class).asSingleton();
				configurator.addBean(BillsDetailDao.class).to(BillsDetailDaoImpl.class).asSingleton();

			}
		};
	}

	/**
	 * Pls make sure you have perform 'sailing.app.AppMain.main(String[])' to launch Application, before call get Application method
	 * 
	 * @return
	 * @see sailing.app.AppMain
	 */
	public static SailingApplication getAppInstance() {
		if (instance == null) {
			logger.error("Fatal: Pls make sure you have perform 'sailing.app.AppMain.main(String[])' to launch Application, before call get Application method");
		}

		return instance;
	}

}
